package jie.controller;

import com.google.code.kaptcha.Constants;
import jie.pojo.Account;
import jie.pojo.Page;
import jie.pojo.Record;
import jie.service.InfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/info")
public class InfoController {

    @Resource
    private InfoService infoService;

    @PostMapping("/login")
    @ResponseBody
    public String login(String cardNo, String password, String code, HttpServletRequest request){
        String oldCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        String msg= "";
        Account user = infoService.login(cardNo, password);

        if(user==null){
            msg= "用户名或密码错误";
        }else if (!oldCode.equals(code)){
            msg = "验证码错误";
        }else if(user.getStatus()==0){
            msg="账号冻结";
        }else {
            msg="成功";
            request.getSession().setAttribute("user", user);
        }

        return msg;
    }

    @PostMapping("/transfer")
    public String transfer(String cardNo2, double balance, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();

        Account user = (Account) request.getSession().getAttribute("user");
        Account card1 = infoService.getByCardNo(user.getCardNo());
        Account card2 = infoService.getByCardNo(cardNo2);
        if(card2==null){
            out.print("<script>alert('转入账户不存在');</script>");
            return "transfer";
        }else if (card1.getBalance()<balance){
            out.print("<script>alert('转出账户余额不足');</script>");
            return "transfer";
        }else if(card2.getStatus()==0){
            out.print("<script>alert('转入账号已冻结');</script>");
            return "transfer";
        }else {
            infoService.transfer(user.getCardNo(),cardNo2,balance);
            user.setBalance(user.getBalance()-balance);
            out.print("<script>alert('转账成功');</script>");

        }
        return "okTransfer";
    }


    @GetMapping("/list")
    public String list(String start,String end,@RequestParam(defaultValue = "1") int pageNo,HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute("user");
        int count = infoService.getCount(start, end, user.getCardNo());
        List<Record> allRecord = infoService.getAllRecord(start, end, user.getCardNo(), pageNo, 2);
        Page page  = new Page(pageNo,2,count);
        page.setList(allRecord);

        request.setAttribute("pageNo",pageNo);
        request.setAttribute("page",page);
        request.setAttribute("start",start);
        request.setAttribute("end",end);
        return "record";
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
       request.getSession().invalidate();
       return "login";
    }

    @PostMapping("updatePwd")
    public String updatePwd(String password,HttpServletRequest request){
        Account user = (Account) request.getSession().getAttribute("user");
        infoService.updatePwd(user.getCardNo(),password);
        return "updatePwd";
    }

}
